// pages/my/myset/myset.js
const db = wx.cloud.database()    //初始化数据库环境
const users = db.collection('users')
var app = getApp()
var user=[]
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo:'',
    isPost:false,
    sex:["男","女"],
    sexIndex:0,
    name:'',
    password:'',
    phone:'',
    isSetInfo:false,
    checked:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      isSetInfo:getApp().globalData.isSetInfo,
      check:getApp().globalData.checked,
      isPost:getApp().globalData.isPost,
      name:getApp().globalData.inspector_name,
      phone:getApp().globalData.inspector_phone,
      sex:getApp().globalData.inspector_sex
    })
    let that = this
    //填充头像
    if (app.globalData.userInfo){
      that.setData({
        userInfo: app.globalData.userInfo
      })
    }else{
      wx.getStorage({
        key: 'userInfo',
        success: function(res) {
          that.setData({
            userInfo: res.data
          })
        },
      })
    }
  },

  
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //接收用户输入的姓名
  inputName: function (e) {
    this.setData({
      name: e.detail.value,
    })
    console.log("姓名:"+this.data.name)
  },

  bind_sex: function (e) {
    console.log(e)
    this.setData({
      sexIndex: e.detail.value
    })
    getApp().globalData.inspector_sex=this.data.sex[this.data.sexIndex];
    console.log('性别发生选择改变，携带值为', this.data.sex[this.data.sexIndex]);
  },
  //手机phone号
  inputPhone: function (e) {
    this.setData({
      phone: e.detail.value,
    })
    console.log("手机:"+this.data.phone)
  },
  //密码
  inputPassword: function (e) {
    this.setData({
      password: e.detail.value,
    })
    console.log("密码:"+this.data.password)
  },
 
  //提交按钮，向数据库中存入数据
  tijiao: function () {
    var phone = "",
    phone =  this.data.phone;
    console.log(phone.length)
    if(this.data.phone==null||this.data.password==null||this.data.name==null||phone.length!=11)
    {
      wx.showModal({
        title: '提示!',
        content: '请输入正确信息',
      })
    }else{
    var that = this;
    console.log("全局"+getApp().globalData.inspector_openid)
    wx.request({
      url:"http://172.17.25.145:8080/trans_inspector",
      method:'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data:{
        openid:getApp().globalData.inspector_openid,
        name:that.data.name,
        phone:that.data.phone,
        password:that.data.password,
        sex:that.data.sex[that.data.sexIndex]
      },
      success: function(res){
        console.log(res)
        getApp().globalData.checked = res.data.data;
        console.log(getApp().globalData.checked)
      },
      fail: function(res){
        console.log(res)
      }
    })
    wx.navigateBack({   //成功后跳转回原页面
      url: '../my'
    })
  }
     
  }
})